
const {User, validateUser} = require('../../model/user');
const bcrypt = require('bcrypt');
module.exports = async (req, res, next) => {
	
	try {
		await validateUser(req.body);
	}catch(ex) {
		return next(JSON.stringify({path: '/admin/user-edit', message: ex.message}));
	}
	let user = await User.findOne({email: req.body.email});
	if(user) {
		return next(JSON.stringify({path: '/admin/user-edit', message: '邮箱地址被占用'}));
	}
	const salt = await bcrypt.genSalt(10);
	const parssword = await bcrypt.hash(req.body.parssword, salt);
	req.body.parssword = parssword;
	await User.create(req.body);
	res.redirect('/admin/user');
}